<template>
  <div style="padding: 10px">
    <!--功能区域-->
    <div style="margin: 10px 0px">

      <el-button type="primary" @click="add" v-if="user.age===1">新增</el-button>
      <!--      <el-button type="primary">导入</el-button>
            <el-button type="primary">导出</el-button>-->

    </div>
    <!--搜索区域-->

    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="请输入关键字" style="width: 20%" clearable/>
      <el-button type="primary" style="margin-left: 10px" @click="load">查询</el-button>
    </div>
    <!--<el-button>按钮</el-button>-->


    <el-table
        border
        stripe
        ref="multipleTable"
        :data="tableData"
        style="width: 80%"
        @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"/>
      <el-table-column property="id" label="ID" width="120" sortable=""/>
      <el-table-column property="name" label="课程名称" width="120"/>
      <el-table-column property="date" label="学分" width="120"/>
      <el-table-column property="author" label="授课老师" width="120"/>

      <el-table-column property="createTime" label="选课开始时间" width="160"/>
      <el-table-column property="endTime" label="选课截止时间" width="160"/>
      <el-table-column property="" label="详情" width="220">

        <template #default="scope" >
          <div v-if="scope.row.isShow" >
            <el-button size="small" @click="handleEdit(scope.row)" v-if="user.age===1">编辑</el-button>
            <el-popconfirm title="确定要删除吗？" @confirm="handleDelete(scope.row.id)" v-if="user.age===1">
              <template #reference>
                <el-button type="text" size="small" >删除</el-button>
              </template>
            </el-popconfirm>
          </div>
          <div v-else>不在选课时间内
            <el-button size="small" @click="handleEdit(scope.row)" v-if="user.age===1">编辑</el-button>
          </div>
          <!--          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>-->
        </template>
      </el-table-column>

    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
          v-model:currentPage="currentPage"
          :page-sizes="[5,10,20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      >
      </el-pagination>

      <el-dialog
          v-model="dialogVisible"
          title="Tips"
          width="30%"
      >

        <el-form :model="form" label-width="120px">
          <el-form-item label="课程名称">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
        </el-form>


        <el-form :model="form" label-width="120px">
          <el-form-item label="学分">
            <el-input v-model="form.date"></el-input>
          </el-form-item>
        </el-form>

        <el-form :model="form" label-width="120px">
          <el-form-item label="授课老师">
            <el-input v-model="form.author"></el-input>

          </el-form-item>
        </el-form>

        <el-form :model="form" label-width="120px">
          <el-form-item label="选课开始时间">
            <el-date-picker
                v-model="form.createTime"
                value-format="YYYY-MM-DD "
                type="date" style="width: 80%"
                            clearable>
            </el-date-picker>
          </el-form-item>
        </el-form>
        <el-form :model="form" label-width="120px">
          <el-form-item label="选课结束时间">
            <el-date-picker v-model="form.endTime"
                            value-format="YYYY-MM-DD "
                            type="date" style="width: 80%"
                            clearable
                            @change="change">
            </el-date-picker>
          </el-form-item>
        </el-form>


        <template #footer>
            <span class="dialog-footer">
              <el-button @click="dialogVisible = false">取消</el-button>
              <el-button type="primary" @click="save"
              >确定</el-button
              >

            </span>
        </template>

      </el-dialog>
    </div>
    <div style="margin-top: 20px">
      <el-button
          @click="toggleSelection([tableData[0],tableData[2],tableData[1]])"
      >选择专业必修课
      </el-button
      >
      <el-button @click="toggleSelection()">清空选课</el-button>
      <el-button @click="saveCourse">确认选课</el-button>
    </div>
  </div>


</template>

<script>
// @ is an alias to /src

import moment from 'moment'
import request from "../utils/request";

export default {
  name: 'Book',
  components: {},
  data() {
    return {
      timeSection: [null, null],
      form: {},
      user:{},
      nowTime: new Date(), // 当前时间
      dialogVisible: false,
      search: '',
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      multipleSelection: [],
    }
  },
  created() {
    let userStr = sessionStorage.getItem("user") || "{}"
    this.user = JSON.parse(userStr)
    this.load()
  },
  computed: {
    formatDate: function () {
      const moment = require("moment");
      return moment().format("YYYY-MM-DD");
    }
  },
  methods: {
    load() {//user/selectAllUsers

      request.get("/book", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data.records.map(item => {
          return {
            ...item,
            isShow: moment(moment().format('YYYY-MM-DD')).isAfter(item.createTime) && moment(moment().format('YYYY-MM-DD')).isBefore(item.endTime)
          }
        })
        console.log(moment().format('YYYY-MM-DD'))

        this.total = res.data.total
        console.log(this.tableData)
        //this.load()//add01
      })
    },
    add() {

      this.dialogVisible = true
      this.form = {}//清空原有属性

    },
    save() {
      if (this.form.id) {
        request.put("/book", this.form).then(res => {
          console.log(res)
          this.$message({
            type: "success",
            message: "更新成功！ :)"
          })
          this.load()
          this.dialogVisible = false
        })
      } else {
        request.post("/book", this.form).then(res => {
          console.log(res)
          this.$message({
            type: "success",
            message: "新增成功！ :)"
          })
          this.load()
          this.dialogVisible = false
        })
      }

    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    saveCourse(rows){

      this.$message.success("选课成功")

    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true;

    },
    handleDelete(id) {
      console.log(id)
      request.delete("/book/" + id).then(res => {
        this.load()
      })
    },
    handleSizeChange() {

    },
    handleCurrentChange() {

    },

  },

}

</script>






<!--
<template>
  <div style="padding: 10px">
    &lt;!&ndash;功能区域&ndash;&gt;
    <div style="margin: 10px 0px">

      <el-button type="primary" @click="add" v-if="user.age===1">新增</el-button>
&lt;!&ndash;      <el-button type="primary">导入</el-button>
      <el-button type="primary">导出</el-button>&ndash;&gt;

    </div>
    &lt;!&ndash;搜索区域&ndash;&gt;

    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="请输入关键字" style="width: 20%" clearable/>
      <el-button type="primary" style="margin-left: 10px" @click="load">查询</el-button>
    </div>
    &lt;!&ndash;<el-button>按钮</el-button>&ndash;&gt;


    <el-table
        border
        stripe
        ref="multipleTable"
        :data="tableData"
        style="width: 85%"
        @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"/>
      <el-table-column property="id" label="ID" width="120" sortable=""/>
      <el-table-column property="name" label="课程名称" width="120"/>
      <el-table-column property="describe1" label="课程描述" width="120"/>
      <el-table-column property="author" label="授课老师" width="120"/>

      <el-table-column property="createTime" label="选课开始时间" width="160"/>
      <el-table-column property="endTime" label="选课截止时间" width="160"/>
      <el-table-column property="" label="详情" width="240">

        <template #default="scope" >
          <div v-if="scope.row.isShow" >
            <el-button size="small" @click="handleEdit(scope.row)" v-if="user.age===1">编辑</el-button>
            <el-popconfirm title="确定要删除吗？" @confirm="handleDelete(scope.row.id)">
              <template #reference>
                <el-button type="text" size="small" v-if="user.age===1" >删除</el-button>
              </template>
            </el-popconfirm>
          </div>
          <div v-else>
            不在选课时间内
&lt;!&ndash;            <el-button size="small" @click="handleEdit(scope.row)" v-if="user.age===1">编辑</el-button>&ndash;&gt;
&lt;!&ndash;            <el-popconfirm title="确定要删除吗？" @confirm="handleDelete(scope.row.id)">
              <template #reference>
                <el-button type="text" size="small" v-if="user.age===1" >删除</el-button>
              </template>
            </el-popconfirm>&ndash;&gt;
          </div>
&lt;!&ndash;          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>&ndash;&gt;
        </template>
      </el-table-column>

    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
          v-model:currentPage="currentPage"
          :page-sizes="[5,10,20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      >
      </el-pagination>

      <el-dialog
          v-model="dialogVisible"
          title="Tips"
          width="30%"
      >

        <el-form :model="form" label-width="120px">
          <el-form-item label="课程名称">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
        </el-form>


        <el-form :model="form" label-width="120px">
          <el-form-item label="课程描述">
            <el-input v-model="form.describe1"></el-input>
          </el-form-item>
        </el-form>

        <el-form :model="form" label-width="120px">
          <el-form-item label="授课老师">
            <el-input v-model="form.author"></el-input>

          </el-form-item>
        </el-form>

        <el-form :model="form" label-width="120px">
          <el-form-item label="选课开始时间">
            <el-date-picker v-model="form.createTime" value-format="YYYY-MM-DD " type="date" style="width: 80%"
                            clearable>
            </el-date-picker>
            &lt;!&ndash;            <el-input type="textarea" v-model="form.createTime"></el-input>&ndash;&gt;
          </el-form-item>
        </el-form>
        <el-form :model="form" label-width="120px">
          <el-form-item label="选课结束时间">
            <el-date-picker v-model="form.endTime" value-format="YYYY-MM-DD " type="date" style="width: 80%" clearable
                            @change="change">
            </el-date-picker>
          </el-form-item>
        </el-form>


        <template #footer>
            <span class="dialog-footer">
              <el-button @click="dialogVisible = false">取消</el-button>
              <el-button type="primary" @click="save"
              >确定</el-button
              >
            </span>
        </template>

      </el-dialog>
    </div>
    <div style="margin-top: 20px">
      <el-button @click="toggleSelection([tableData[4],tableData[0],tableData[1],tableData[2]])"
      >选择专业必修课
      </el-button
      >
      <el-button @click="toggleSelection()">清空选课</el-button>
      <el-button @click="saveCourse()">确认选课</el-button>
    </div>
  </div>


</template>

<script>
// @ is an alias to /src

import moment from 'moment'
import request from "../utils/request";

export default {
  name: 'Book',
  components: {},
  data() {
    return {
      timeSection: [null, null],
      form: {},
      user:{},
      nowTime: new Date(), // 当前时间
      dialogVisible: false,
      search: '',
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      multipleSelection: [],
    }
  },
  created() {
    let userStr = sessionStorage.getItem("user") || "{}"
    this.user = JSON.parse(userStr)
    this.load()
  },
  computed: {
    formatDate: function () {
      const moment = require("moment");
      return moment().format("YYYY-MM-DD");
    }
  },
  methods: {
    load() {//user/selectAllUsers

      request.get("/book", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data.records.map(item => {
          return {
            ...item,
            isShow: moment(moment().format('YYYY-MM-DD')).isAfter(item.createTime) && moment(moment().format('YYYY-MM-DD')).isBefore(item.endTime)
          }
        })
        console.log(moment().format('YYYY-MM-DD'))

        this.total = res.data.total
        console.log(this.tableData)
        //this.load()//add01
      })
    },
    add() {

      this.dialogVisible = true
      this.form = {}//清空原有属性

    },
    save() {
      if (this.form.id) {
        request.put("/book", this.form).then(res => {
          console.log(res)
          this.$message({
            type: "success",
            message: "更新成功！ :)"
          })
          this.load()
          this.dialogVisible = false
        })
      } else {
        request.post("/book", this.form).then(res => {
          console.log(res)
          this.$message({
            type: "success",
            message: "新增成功！ :)"
          })
          this.load()
          this.dialogVisible = false
        })
      }

    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    saveCourse(){

      this.$message.success("选课成功")

    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true;

    },
    handleDelete(id) {
      console.log(id)
      request.delete("/book/" + id).then(res => {
        this.load()
      })
    },
    handleSizeChange() {

    },
    handleCurrentChange() {

    },

  },

}

</script>

-->
